$(function () {

    let $btn = $("button")
    let $name = $("#name");
    let $password = $("#password");
    let $turepassword = $("#turepassword");
    let $promname = $(".promptname")
    let $prompass = $(".promptpass")
    let $promturepass = $(".promptturepass")
    let $promture = $(".promptture")
    let $checkbox = $(".checkbox")

    $name.blur(function () {
        var reg = /^\w{4,16}$/
        if (reg.test($name.val())) {
            $promname.removeClass("active")
        } else {
            $promname.addClass("active")
        }
    })
    $password.blur(function () {
        var reg = /^\w{6,16}$/
        if (reg.test($password.val())) {
            $password.css({
                borderColor: "green"
            })
            $prompass.removeClass("active")
        } else {
            $password.css({
                borderColor: "red"
            })
            $prompass.addClass("active")
        }
    })
    $turepassword.blur(function () {
        if ($password.val() === $turepassword.val()) {
            $password.css({
                borderColor: "green"
            })
            $turepassword.css({
                borderColor: "green"
            })
            $promturepass.removeClass("active")
        } else {
            $password.css({
                borderColor: "red"
            })
            $turepassword.css({
                borderColor: "red"
            })
            $promturepass.addClass("active")
        }
    })


    $btn.click(function (e) {
        e.preventDefault();
        var reg = /^\w{4,16}$/
        var reg2 = /^\w{6,16}$/
        if ($checkbox.prop("checked") === false) {
            $promture.addClass("active")
        } else {
            $promture.removeClass("active")
        }
        if ($password.val() !== $turepassword.val() ||
            $checkbox.prop("checked") === false ||
            !reg.test($name.val()) ||
            !reg2.test($password.val())) {
            alert("用户名或密码不符合规则,请同意条款")
        } else {
            $.ajax({
                type: "POST",
                url: "http://localhost/server/register.php",
                data: {
                    username: $name.val(),
                    password: $password.val()
                },
                dataType: "json",
                success: function (response) {
                    console.log(response.type);
                    if (response.type === "success")
                        window.location.href = "./login.html"

                }
            });
        }


    });





})